package com.ocean.web.controller.product;

import java.util.List;

import com.ocean.common.core.domain.Ztree;
import com.ocean.common.core.domain.entity.SysUser;
import com.ocean.common.utils.ShiroUtils;
import com.ocean.common.utils.StringUtils;
import com.ocean.resources.domain.OResources;
import com.ocean.resources.service.IOResourcesService;
import com.ocean.system.service.ISysConfigService;
import com.ocean.system.service.ISysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ocean.common.annotation.Log;
import com.ocean.common.enums.BusinessType;
import com.ocean.common.core.controller.BaseController;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.utils.poi.ExcelUtil;

/**
 * 资源目录信息Controller
 *
 * @author ocean
 * @date 2022-12-22
 */
@Controller
@RequestMapping("/resources")
public class OResourcesController extends BaseController
{
    private String prefix = "product/resources";

    @Autowired
    private IOResourcesService oResourcesService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysConfigService configService;

    @RequiresPermissions("system:resources:view")
    @GetMapping()
    public String resources()
    {
        return prefix + "/list";
    }

    /**
     * 查询资源目录信息树列表
     */
    //@RequiresPermissions("system:resources:list")
    @PostMapping("/list")
    @ResponseBody
    public List<OResources> list(OResources oResources)
    {
        //当前登录用户是否是单位管理员
        SysUser sysUser = ShiroUtils.getSysUser();
        if (sysRoleService.isDeptAdmin(sysUser)){
            oResources.setDepartId(sysUser.getDeptId().toString());
        }
        List<OResources> list = oResourcesService.selectOResourcesList(oResources);
        return list;
    }

    /**
     * 导出资源目录信息列表
     */
    @RequiresPermissions("system:resources:export")
    @Log(title = "资源目录信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OResources oResources)
    {
        List<OResources> list = oResourcesService.selectOResourcesList(oResources);
        ExcelUtil<OResources> util = new ExcelUtil<OResources>(OResources.class);
        return util.exportExcel(list, "资源目录信息数据");
    }

    /**
     * 新增资源目录信息
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("oResources", oResourcesService.selectOResourcesById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存资源目录信息
     */
    @RequiresPermissions("system:resources:add")
    @Log(title = "资源目录信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OResources oResources)
    {
        //“单位管理员无法在根目录创建资源”
        SysUser sysUser = ShiroUtils.getSysUser();
        if (oResources.getParentId() == null && sysRoleService.isDeptAdmin(sysUser)) {
            return AjaxResult.success("单位管理员无法在根目录创建资源");
        }
        String msg = oResourcesService.insertOResources(oResources,configService.selectConfigByKey("file_path"));
        return AjaxResult.success(msg);
    }

    /**
     * 修改资源目录信息
     */
    @RequiresPermissions("system:resources:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OResources oResources = oResourcesService.selectOResourcesById(id);
        String pDir = configService.selectConfigByKey("file_path");
        if (StringUtils.isNotEmpty(oResources.getParentName()))
            pDir = oResources.getParentResourcesDirPath()+"/";
        oResources.setResourcesDirPath(oResources.getResourcesDirPath().replace(pDir, ""));
        mmap.put("oResources", oResources);
        return prefix + "/edit";
    }

    /**
     * 修改保存资源目录信息
     */
    @RequiresPermissions("system:resources:edit")
    @Log(title = "资源目录信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OResources oResources)
    {
        String msg = oResourcesService.updateOResources(oResources,configService.selectConfigByKey("file_path"));
        return AjaxResult.success(msg);
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:resources:remove")
    @Log(title = "资源目录信息", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        String s = oResourcesService.deleteOResourcesById(id);
        if ("删除成功".equals(s)){
            return AjaxResult.success(s);
        }else if ("删除失败".equals(s)){
            return AjaxResult.error(s);
        }else {
            return AjaxResult.warn(s);
        }
    }

    /**
     * 选择资源目录信息树
     */
    @GetMapping(value = { "/selectResourcesTree/{id}", "/selectResourcesTree/" })
    public String selectResourcesTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("oResources", oResourcesService.selectOResourcesById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载资源目录信息树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = oResourcesService.selectOResourcesTree();
        return ztrees;
    }

}
